﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvdDonk.Bomberman.Menu;
using MvdDonk.Bomberman.Screens.Base;
using MvdDonk.Bomberman.Entities;
using Microsoft.Xna.Framework;

namespace MvdDonk.Bomberman.Screens
{
    /// <summary>
    /// The main menu screen is the first thing displayed when the game starts up.
    /// </summary>
    class MainMenuScreen : MenuScreen
    {
        /// <summary>
        /// Constructor fills in the menu contents.
        /// </summary>
        public MainMenuScreen()
            : base("Main Menu", MenuOrientation.Vertical)
        {
            // Create our menu entries.
            MenuEntry joinGameMenuEntry = new MenuEntry("Join a game");
            MenuEntry hostMenuEntry = new MenuEntry("Host a game");
            MenuEntry optionsMenuEntry = new MenuEntry("Options");
            MenuEntry devStartGameMenuEntry = new MenuEntry("DEV - start game");
            MenuEntry exitMenuEntry = new MenuEntry("Exit");


            optionsMenuEntry.Selectable = false;

            // Hook up menu event handlers.
            joinGameMenuEntry.Selected += JoinGameMenuEntrySelected;
            hostMenuEntry.Selected += HostGameMenuEntrySelected;
            optionsMenuEntry.Selected += OptionsMenuEntrySelected;
            exitMenuEntry.Selected += OnCancel;
            devStartGameMenuEntry.Selected += DevStartGameMenuEntrySelected;

            // Add entries to the menu.
            MenuEntries.Add(joinGameMenuEntry);
            MenuEntries.Add(hostMenuEntry);
            MenuEntries.Add(optionsMenuEntry);
            MenuEntries.Add(devStartGameMenuEntry);
            MenuEntries.Add(exitMenuEntry);
        }

        void DevStartGameMenuEntrySelected(object sender, EventArgs e)
        {
            var player = new Player(Bomberman.Instance.TestPlayerTexture, new Vector2(50, 50), -1, "local");
            Bomberman.Instance.EntityManager.AddPlayer(player);
            ScreenManager.AddScreen(new GameplayScreen());
        }


        /// <summary>
        /// Event handler for when the Play Game menu entry is selected.
        /// </summary>
        void HostGameMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new HostServerScreen());
        }

        /// <summary>
        /// Event handler for when the Options menu entry is selected.
        /// </summary>
        void OptionsMenuEntrySelected(object sender, EventArgs e)
        {
            //ScreenManager.AddScreen(new OptionsMenuScreen(),);
        }

        void JoinGameMenuEntrySelected(object sender, EventArgs e)
        {
            ScreenManager.AddScreen(new JoinServerScreen());
        }

        /// <summary>
        /// When the user cancels the main menu, ask if they want to exit the sample.
        /// </summary>
        protected override void OnCancel()
        {
            const string message = "Are you sure you want to exit this sample?";

            MessageBoxScreen confirmExitMessageBox = new MessageBoxScreen(message);
            confirmExitMessageBox.Accepted += ConfirmExitMessageBoxAccepted;

            ScreenManager.AddScreen(confirmExitMessageBox);
        }

        /// <summary>
        /// Event handler for when the user selects ok on the "are you sure
        /// you want to exit" message box.
        /// </summary>
        void ConfirmExitMessageBoxAccepted(object sender, EventArgs e)
        {
            Bomberman.Instance.Exit();
        }
    }
}
